package leetcode

import (
	"Algorithms/util"
)

//807. 保持城市天际线
func maxIncreaseKeepingSkyline(grid [][]int) int {
	n:=len(grid)
	var max_i,max_j[]int
	for i:=0;i<n;i++{
		max_i=append(max_i, -1)
		max_j=append(max_j, -1)
	}
	for i:=0;i< n;i++{
		for j:=0;j<n;j++{
			if grid[i][j]>max_i[i]{
				max_i[i]=grid[i][j]
			}
			if grid[i][j]>max_j[j]{
				max_j[j]=grid[i][j]
			}
		}
	}
	diff:=0
	for i:=0;i< n;i++ {
		for j := 0; j < n; j++ {
			diff+=util.Min(max_i[i],max_j[j])-grid[i][j]
		}
	}
	return diff
}